<template>
	<view class="container">
	  <view class="wechatapp">
	    <view class="header">
	      <open-data class="" type="userAvatarUrl"></open-data>
		  <open-data class="" type="userAvatarUrl"></open-data>
	    </view>
	  </view>
	  <view class="auth-title">申请获取以下权限</view>
	  <view class="auth-subtitle">获得你的公开信息（昵称、头像等）</view>
	  <view class="login-btn">
	    <button class="btn-normal" openType="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo">授权登录</button>
	  </view>
	  <view class="no-login-btn">
	    <button class="btn-normal" @tap="onNotLogin">暂不登录</button>
	  </view>
	 
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			//授权登陆
			getUserInfo(e){
				let _this=this;
				 uni.getUserInfo({
				       success: function (infoRes) {
				        var names=infoRes.userInfo.nickName;
						
						uni.login({
						      success(res) {
								  console.log(res)
								  
								  
								  uni.showLoading({
								      title: '登陆中...'
								  });
								  
								  var arr= {
								  		code:res.code,
								  		name: names,
								  	};
									arr=JSON.stringify(arr)
								  _this.$http({
								  	url: '/IWeiXinMiniProgramService.login',
								  	method: 'post',
								  	data: arr
								  }).then((res) => {
								  	uni.hideLoading();
								  	console.log(res);
								  	if (res.code == '0') {
	
								  		uni.setStorageSync('user', res.data);
										if(res.data.customer && res.data.customer.level){
											uni.setStorageSync('level',res.data.customer.level)
										}
								  		uni.showToast({
								  		    title: '登陆成功',
								  		    duration: 1000,
								  			icon:'none'
								  		});
										setTimeout(function(){
											uni.navigateBack()
										},1000)
								  	}else{
								  		uni.showToast({
								  		    title: res.data,
								  		    duration: 1000,
								  			icon:'none'
								  		});	
								  	}
								  }).catch((err) => {
								  	uni.hideLoading();
								  	console.log(err)
								  
								  })
						        
						      }
						    });
				      }
				    });
				console.log(e)
				
				
			},
			//绑定手机号
			getPhoneNumber(e){
				console.log(e)
				uni.login({
				      success(res) {
						  console.log(res)
						  
						  
						  uni.showLoading({
						      title: '登陆中...'
						  });
						  
						  var arr= {
						  		code:res.code,
						  		name: names,
						  	};
							arr=JSON.stringify(arr)
						  _this.$http({
						  	url: '/IWeiXinMiniProgramService.decode',
						  	method: 'post',
						  	data: arr
						  }).then((res) => {
						  	uni.hideLoading();
						  	console.log(res);
						  	if (res.status == 'ok' && res.data) {
						  		
						  		
						  		
						  	}else{
						  		uni.showToast({
						  		    title: res.data,
						  		    duration: 1000,
						  			icon:'none'
						  		});	
						  	}
						  }).catch((err) => {
						  	uni.hideLoading();
						  	console.log(err)
						  
						  })
				        
				      }
				    });
				// 发送用户信息
				// App._post_form('user/login', {
				//   code: res.code,
				//   user_info: e.detail.rawData,
				//   encrypted_data: e.detail.encryptedData,
				//   iv: e.detail.iv,
				//   signature: e.detail.signature,
				//   referee_id: wx.getStorageSync('referee_id')
				// }, result => {
				//   // 记录token user_id
				//   wx.setStorageSync('token', result.data.token);
				//   wx.setStorageSync('user_id', result.data.user_id);
				//   // 执行回调函数
				//   callback && callback();
				// }, false, () => {
				//   wx.hideLoading();
				// });
			},
			//取消登陆
			onNotLogin(){
				uni.navigateBack()
			}
		}
	}
</script>

<style>
page {
  background: #fff;
  font-size: 32rpx;
}

.container {
  padding: 0 60rpx;
}

.wechatapp {
  padding: 80rpx 0 48rpx;
  border-bottom: 1rpx solid #e3e3e3;
  margin-bottom: 72rpx;
  text-align: center;
}

.wechatapp .header {
  width: 190rpx;
  height: 190rpx;
  border: 2px solid #fff;
  margin: 0rpx auto 0;
  border-radius: 50%;
  overflow: hidden;
  box-shadow: 1px 0px 5px rgba(50, 50, 50, 0.3);
}

.auth-title {
  color: #585858;
  font-size: 34rpx;
  margin-bottom: 40rpx;
}

.auth-subtitle {
  color: #888;
  margin-bottom: 88rpx;
  font-size: 28rpx;
}

.login-btn {
  padding: 0 20rpx;
}

.login-btn button {
  height: 88rpx;
  line-height: 88rpx;
  background: #04be01;
  color: #fff;
  font-size: 30rpx;
  border-radius: 999rpx;
  text-align: center;
}

.no-login-btn {
  margin-top: 20rpx;
  padding: 0 20rpx;
}

.no-login-btn button {
  height: 88rpx;
  line-height: 88rpx;
  background: #dfdfdf;
  color: #fff;
  font-size: 30rpx;
  border-radius: 999rpx;
  text-align: center;
}

</style>
